package cn.yr.blog.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.yr.blog.mapper.ArticleMapper;
import cn.yr.blog.service.ArticleService;
import cn.yr.blog.service.ArticleTagService;
import cn.yr.blog.service.TagService;
import cn.yr.common.core.domain.dto.YrArticleTagDto;
import cn.yr.common.core.domain.entity.YrArticle;
import cn.yr.common.core.domain.entity.YrArticleTag;
import cn.yr.common.core.domain.entity.YrTag;
import cn.yr.common.core.domain.vo.PageVo;
import cn.yr.search.api.ArticleSearchApi;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 文章(YrArticle)表服务实现类
 *
 * @author makejava
 * @since 2020-03-05 20:21:24
 */
@Service("articleService")
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, YrArticle> implements ArticleService {

    @Resource
    private ArticleMapper articleMapper;
    @Resource
    private TagService tagService;
    @Autowired
    private ArticleTagService articleTagService;


    @Override
    public PageVo<YrArticleTagDto> pageArticle(Page<YrArticleTagDto> page, QueryWrapper<YrArticleTagDto> articleQueryWrapper) {
        return articleMapper.pageArticle(page,articleQueryWrapper);
    }

    @Override
    public YrArticleTagDto getArticleById(Integer id) {
        return articleMapper.getArticleById(id);
    }

    @Transactional(rollbackFor=Exception.class)
    @Override
    public void addArticle(YrArticleTagDto yrArticleTagDto) {

        List<YrTag> yrTags = yrArticleTagDto.getYrTags();
        if(ObjectUtil.isEmpty(yrTags)){
            this.save(yrArticleTagDto);
            return;
        }
        List<YrTag> saveTags = yrTags.stream().filter(tag -> ObjectUtil.isNotEmpty(tag.getId())).collect(Collectors.toList());
        List<YrTag> nameTags = yrTags.stream().filter(tag -> StrUtil.isNotEmpty(tag.getName())).collect(Collectors.toList());
        nameTags.forEach(tag->{
            String randomTagColor = tagService.getRandomTagColor();
            tag.setColor(randomTagColor);
            tag.setCreateTime(new Date());
            saveTags.add(tag);
        });
        tagService.saveBatch(nameTags);
        this.save(yrArticleTagDto);
        ArrayList<YrArticleTag> yrArticleTags = new ArrayList<>(saveTags.size());
        saveTags.forEach(tag->{
            YrArticleTag yrArticleTag = new YrArticleTag();
            yrArticleTag.setTagId(tag.getId());
            yrArticleTag.setArticleId(yrArticleTagDto.getId());
            yrArticleTags.add(yrArticleTag);
        });
        articleTagService.saveBatch(yrArticleTags);
    }

}